import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import welcome from '../components/welcome.vue'
import authority from '../components/authority.vue'
import menus from '../components/menus.vue'
import student from '../components/student.vue'
import user from '../components/user.vue'
import register from '../components/register.vue'
Vue.use(VueRouter)

const routes = [
  {path:"/",redirect:"/login"},
  {path:"/login",    component:Login  },
  {path:"/home",   
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:welcome},
      {path:'/stu/getAuthority',component:authority},
      {path:'/stu/Menus',component:menus},
      {path:'/stu/students',component:student},
      {path:'/stu/users',component:user}
    ]
  },
  {path:'/register',component:register},
]

const router = new VueRouter({
  routes
})


//这是一个路由守卫
router.beforeEach((to,from,next)=>{
  //to 将要访问的路径
  //from 从哪个路由来
  //next 表示一个函数，是否放行，next() 放行，next('/login) 强制跳转

  if (to.path==='/login') return next()
  if (to.path==='/register') return next()
  // token
  const tokenstr=window.sessionStorage.getItem('token')
  if (!tokenstr) return next('/login')
  next()
})
export default router
